//
//  HMNewFeatureViewController.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

/// cell的数量
private let ItemCount = 4

class HMNewFeatureViewController: UICollectionViewController {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 布局属性
    let layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.register(HMNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // 准备layout
        prepareLayout()
    }
    
    /**
     准备CollectionView 的layout
     */
    private func prepareLayout() {
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal
        
        collectionView?.isPagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMNewFeatureCell
        
        // 设置当前cell是第几个cell
        cell.imageIndex = indexPath.item
        
        if indexPath.item == ItemCount - 1 {
            cell.setButtonHidden(isHidden: false)
        } else {
            cell.setButtonHidden(isHidden: true)
        }
        
        return cell
    }
}

class HMNewFeatureCell: UICollectionViewCell {
    
    // 不同页面显示不同图片
    var imageIndex: Int? = 0 {
        // 当collectionView 来设置imageIndex的时候,根据imageIndex去加载图片
        didSet {
            backgroundImageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }
    
    func setButtonHidden(isHidden: Bool) -> Void {
        shareButton.isHidden = isHidden
        startButton.isHidden = isHidden
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 准备UI
        setupUI()
    }
    
    // MARK: - 准备UI
    private func setupUI() {
        // 添加为子控件
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(shareButton)
        contentView.addSubview(startButton)
        
        // 添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        shareButton.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        // leading
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
        // top
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
        // trailing
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
        
        // 开始按钮
        // centerX
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        //
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -120))
        
        // 分享按钮
        // centerX
        self.contentView.addConstraint(NSLayoutConstraint(item: shareButton, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.startButton, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: shareButton, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.startButton, attribute: NSLayoutAttribute.top, multiplier: 1, constant: -16))
    }
    
    // MARK: - 按钮点击事件
    /// 分享按钮被点击
    func didClickShareButton() {
        self.shareButton.isSelected = !self.shareButton.isSelected
    }
    
    /// 开始按钮被点击
    func didClickStartButton() {
        // TODO:切换到HMMainTabbarController
//        let appDelegate = UIApplication.shared.delegate as! AppDelegate
//        appDelegate.switchRootViewController(controller: HMMainTabbarController())
        AppDelegate.outSwitchRootViewController(controller: HMMainTabbarController())
    }
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var backgroundImageView: UIImageView = UIImageView()
    
    /// 分享按钮
    lazy var shareButton: UIButton = {
        let button = UIButton()
        
        // 设置文字
        button.setTitle("分享给大家", for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        button.setImage(UIImage(named: "new_feature_share_false"), for: UIControlState.normal)
        button.setImage(UIImage(named: "new_feature_share_true"), for: UIControlState.selected)
        
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        // 点击事件
        button.addTarget(self, action: #selector(didClickShareButton), for: UIControlEvents.touchUpInside)
        
        return button
    }()
    
    /// 开始按钮
    lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置文字
        button.setTitle("开始体验", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: UIControlState.highlighted)
        
        // 点击事件
        button.addTarget(self, action: #selector(didClickStartButton), for: UIControlEvents.touchUpInside)
        
        return button
    }()
}
